package com.young.android.common;

import com.young.android.R;
import com.young.android.dbreposit.SqliteHelper;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

public abstract class BaseActivity extends Activity {

    private boolean bAllowExitApplication = false;
    private boolean bQuitHint = false;
    protected Context ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
      // TODO Auto-generated method stub
      try
      {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        ctx = this;
        SqliteHelper.mContext=ctx;

        init();
      } catch (Exception e)
      {
        e.printStackTrace();
      }
    }

    protected abstract void init();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
      // TODO Auto-generated method stub
      if (bAllowExitApplication)
      {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
        {
          if (this.bQuitHint)
          {
            // 退出程序
            PubFunction.ExitApplication(this);
          } else
          {
            // 提示
            bQuitHint = true;
            PubFunction.ShowToast(this, R.string.quit_out_hint);
            return false;
          }

        }
      }
      return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStop()
    {
      // TODO Auto-generated method stub
      super.onStop();
      bQuitHint = false;
    }

    /**
     * 是否允许退出程序
     * 
     * @param bAllow
     */
    protected void SetAllowExitApplication(boolean bAllow)
    {
      bAllowExitApplication = bAllow;
    }
    
}
